import java.util.concurrent.BlockingDeque;

public class PCMode {
    public static void main(String[] args) throws InterruptedException {
        MyBlockingDeque myBlockingDeque = new MyBlockingDeque();
        myBlockingDeque.put(1);
        System.out.println(myBlockingDeque.take());
    }
}

class MyBlockingDeque {
    int[] nums = new int[]{1000};
    int head = 0,tail = 0;
    volatile int size = 0;

    public void put(int value) throws InterruptedException {
        synchronized (this) {
            while (size == nums.length) {
                this.wait();
            }

            tail = (tail + 1) % nums.length;
            nums[tail] = value;
            size++;
            this.notifyAll();
        }
    }
    public int take() throws InterruptedException {
        int t = -1;
        synchronized (this) {
            while (size == 0) {
                this.wait();
            }
            t = nums[head];
            head = (head + 1) % nums.length;
            size--;
            this.notifyAll();
        }
        return t;
    }
}
